$(function(){
    var contentManager = new ContentManager();
    
    contentManager.init().init();
});



function ContentManager(){
    this.init = function(){
        return CONTENTMANAGER;
    };
    
    var CONTENTMANAGER = {
        init : function(){
            this.funs.initUpload();
            CONTENTMANAGER.listeners.bindEvents();
        },
        listeners : {
            bindEvents : function(){
                $.util.foreachFunction(this, [ "bindEvents" ]);
            },
            bindFormSubmit : function(){
                $("#btn_notice_add_submit").click(function(){
                    
                    var $form = $("#frm_app");
                    if('' == $.trim($("#hid_subject_content_images").val())){
                        alert('未上传图片');
                        return;
                    }


                    $form.submit();
                });
            },
            bindUpload : function(){
                $("#btn_subject_content_upload_enter").click(function(){
                    //清空页面上的图片
                    CONTENTMANAGER.funs.initElement($("#hid_subject_content_images"));
                    
                    $('#btn_subject_content_upload').uploadifyUpload();
                });
            },
            bindForwardSubject : function(){
                $("#a_subject").click(function(){
                    $.util.initHiddenFormSubmit($('#frm_hidden'), SYSTEM_DATA.contextPath + '/Subject/scienceManager.shtml', [{
                        'name' : 'id', 
                        'value' : $(this).attr('sid')
                    }]);
                });
            },
            bindImagesEmpty : function(){
                $("#btn_subject_content_empty").click(function(){
                    $.post(SYSTEM_DATA.contextPath + '/Subject/subjectClearImages.shtml', {
                        'id' : SC.id
                    }, function(jsonResult){
                        if(0 == jsonResult.status){
                            alert(jsonResult.info);
                        }
                        $("#hid_subject_content_images").val('');
                        
                    }, 'json');
                    
                });
            }
        },
        funs : {
            initUpload : function(){
                $('#btn_subject_content_upload').uploadify({
                    'uploader'  : SYSTEM_DATA.public_path + '/js/plugin/Uploadify/uploadify.swf',
                    'script'    : SYSTEM_DATA.contextPath + '/File/upload',
                    'cancelImg' : SYSTEM_DATA.public_path + '/js/plugin/Uploadify/cancel.png',
                    'folder'    : '/uploads',
                    'removeCompleted' : true,
                    'auto'       : false,
                    'multi': true,
                    'fileExt' : '*.jpg;*.jpeg;*.png;*.gif',
                    'fileDesc' : '可上传的图片文件',
                    'sizeLimit' : 1024 * 1024 * 2,
                    //'buttonText' : '上传',
                    'onComplete'  : function(event, ID, fileObj, response, data) {
                        //console.debug(response);
                        //console.debug(data);
                        response = $.parseJSON(response);
                        //var lastId = response.data;
                        console.debug(response);
                        //$fileId = $('#hid_file_id');
                        $("#hid_subject_content_images").val($("#hid_subject_content_images").val() + response.data.path + ',');
                    //$fileId.val('' == $fileId.val() ? lastId : $fileId.val() + ',' + lastId);
                    },
                    'onError' : function(event,ID,fileObj,errorObj) {
                        console.debug(errorObj);
                    }
                });
            },
            initElement : function($element){
                $element.val('');
            }
        }
    };
}


